import 'package:abc/controller/follow_controller.dart';
import 'package:abc/model/user.dart';
import 'package:abc/view/CustomAppBar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class FollowPage extends GetView<FollowController> {
  const FollowPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
       if (!Get.isRegistered<FollowController>()) {
      Get.lazyPut(() => FollowController());
    }
    return Scaffold(
      backgroundColor: const Color(0xFF0E0F26), // 深色背景
      appBar: CustomAppBar(
        title: controller.pageTitle, // 根据页面类型显示标题
        onBack: () {
          Get.back();
        },
      ),
      body: _buildSmartRefresher(),
    );
  }
  
  Widget _buildSmartRefresher() {
    return SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: WaterDropHeader(
          complete: Text("刷新成功", style: TextStyle(color: Colors.white)),
          idleIcon: Text("下拉刷新", style: TextStyle(color: Colors.white)),
          refresh: Text("正在刷新...", style: TextStyle(color: Colors.white)),
          failed: Text("刷新失败", style: TextStyle(color: Colors.white)),
        ),
        footer: ClassicFooter(
          idleText: "上拉加载更多",
          loadingText: "正在加载更多...",
          noDataText: "已加载全部记录",
          failedText: "加载失败，请点击重试",
          canLoadingText: "松手加载更多",
          textStyle: TextStyle(color: Colors.white),
        ),
        controller: controller.refreshController,
        onRefresh: controller.onRefresh,
        onLoading: controller.onLoading,
        child: ListView.builder(
          padding: EdgeInsets.only(top: 10),
          itemBuilder: (c, i) => _buildUserItem(controller.items[i], i, controller.items.length),
          itemCount: controller.items.length,
        ),
      );
  }
  
  // 用户列表项组件
  Widget _buildUserItem(User user, int index, int total) {
    // 根据位置确定圆角
    BorderRadius borderRadius = BorderRadius.zero;
    if (total == 1) {
      borderRadius = BorderRadius.circular(10);
    } else if (index == 0) {
      borderRadius = const BorderRadius.vertical(top: Radius.circular(10));
    } else if (index == total - 1) {
      borderRadius = const BorderRadius.vertical(bottom: Radius.circular(10));
    }
    
    return GestureDetector(
      onTap: () {
        // 添加点击事件处理
        Get.snackbar('提示', '点击了用户: ${user.username}');
        // TODO: 在这里添加用户点击后的具体操作，比如跳转到用户详情页面
      },
      child: Container(
        padding: EdgeInsets.only(left: 10,top: 15,bottom: 15),
          decoration: BoxDecoration(
              color: const Color(0x33959595), 
              borderRadius: borderRadius,
            ),
          child:  Row(
           children: [
             // 用户头像
             ClipOval(
               child: Image.asset(
                 user.avatar,
                 width: 40,
                 height: 40,
                 fit: BoxFit.cover,
               ),
             ),
             const SizedBox(width: 8),
             // 用户信息
             Expanded(
               child: Column(
                 crossAxisAlignment: CrossAxisAlignment.start,
                 children: [
                   Text(
                     user.username,
                     style: const TextStyle(
                       color: Colors.white,
                       fontSize: 15,
                       fontWeight: FontWeight.w600,
                     ),
                   ),
                   const SizedBox(height: 5),
                   Text(
                     user.signature,
                     style: TextStyle(
                       color: Colors.white60,
                       fontSize: 13,
                       fontWeight: FontWeight.w400,
                       overflow: TextOverflow.ellipsis,
                     ),
                     maxLines: 1,
                   ),
                 ],
               ),
             ),
             // 右侧箭头
             Image.asset(
                  'assets/images/ic_arrow_right.png',
                  width: 24,
                  height: 24,
                ),
           ],
        ),).paddingSymmetric(horizontal: 16),
    );
  }
}